package com.jianbing.shasha.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jianbing.shasha.constant.ShashaTestAnswerConstant;
import com.jianbing.shasha.domain.ShashaAccuracyVo;
import com.jianbing.shasha.domain.ShashaRecordVO;
//import org.bytedeco.javacv.FFmpegFrameGrabber;
//import org.bytedeco.javacv.FrameGrabber;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianbing.shasha.mapper.ShashaUserRecordMapper;
import com.jianbing.shasha.domain.ShashaUserRecord;
import com.jianbing.shasha.service.IShashaUserRecordService;
import org.springframework.transaction.annotation.Transactional;

import javax.print.DocFlavor;

/**
 * 用户学习测试记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-09
 */
@Service
public class ShashaUserRecordServiceImpl implements IShashaUserRecordService 
{
    @Autowired
    private ShashaUserRecordMapper shashaUserRecordMapper;

    /**
     * 查询用户学习测试记录
     * 
     * @param id 用户学习测试记录主键
     * @return 用户学习测试记录
     */
    @Override
    public ShashaUserRecord selectShashaUserRecordById(Long id)
    {
        return shashaUserRecordMapper.selectShashaUserRecordById(id);
    }

    /**
     * 查询用户学习测试记录列表
     * 
     * @param shashaUserRecord 用户学习测试记录
     * @return 用户学习测试记录
     */
    @Override
    public List<ShashaUserRecord> selectShashaUserRecordList(ShashaUserRecord shashaUserRecord)
    {
        return shashaUserRecordMapper.selectShashaUserRecordList(shashaUserRecord);
    }

    /**
     * 新增用户学习测试记录
     * 
     * @param shashaUserRecord 用户学习测试记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertShashaUserRecord(ShashaUserRecord shashaUserRecord)
    {
//        shashaUserRecordMapper.insertShashaUserRecord(shashaUserRecord);
        Long questionId =  shashaUserRecord.getQuestionId();
        shashaUserRecordMapper.reviewWrongRecord(shashaUserRecord.getUserId(),questionId);
        return shashaUserRecordMapper.insertShashaUserRecord(shashaUserRecord);
    }

    /**
     * 修改用户学习测试记录
     * 
     * @param shashaUserRecord 用户学习测试记录
     * @return 结果
     */
    @Override
    public int updateShashaUserRecord(ShashaUserRecord shashaUserRecord)
    {
        return shashaUserRecordMapper.updateShashaUserRecord(shashaUserRecord);
    }

    /**
     * 批量删除用户学习测试记录
     * 
     * @param ids 需要删除的用户学习测试记录主键
     * @return 结果
     */
    @Override
    public int deleteShashaUserRecordByIds(Long[] ids)
    {
        return shashaUserRecordMapper.deleteShashaUserRecordByIds(ids);
    }

    /**
     * 删除用户学习测试记录信息
     * 
     * @param id 用户学习测试记录主键
     * @return 结果
     */
    @Override
    public int deleteShashaUserRecordById(Long id)
    {
        return shashaUserRecordMapper.deleteShashaUserRecordById(id);
    }

    @Override
    public List<ShashaAccuracyVo> getAccuracy(ShashaAccuracyVo shashaAccuracyVo) {
        return shashaUserRecordMapper.selectAccuracy(shashaAccuracyVo);
    }

    @Override
    public List<ShashaRecordVO> selectShashaStudyRecord(ShashaRecordVO recordVO) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(3,2);
        map.put(2,3);
        map.put(4,4);
        List<ShashaRecordVO> list = shashaUserRecordMapper.selectShashaUserStudyRecord(recordVO);
        for(ShashaRecordVO record:list){
            if(record.getAnswerPath()==null){
                continue;
            }
            String answer = null;
            Long type =  (record.getLevels()-1)%4;
            if(type==0){
                answer = ShashaTestAnswerConstant.HAPPY;
            }else if (type==1){
                answer = ShashaTestAnswerConstant.SAD;
            } else if (type==2) {
                answer = ShashaTestAnswerConstant.ANGRY;
            }else {
                answer = ShashaTestAnswerConstant.FEAR;
            }
            record.setRightAnswer(answer);
            if("test".equals(record.getTestType())){
                if(record.getAnswerPath()!=null){
                    if(record.getAnswerPath().length()>7 && "mp3".equals(record.getAnswerPath().substring(record.getAnswerPath().length()-3))){
                        record.setVoiceAnswer(record.getAnswerPath());
                        record.setVoiceDuration(getMp3Time(record.getAnswerPath())+" ms");
                    }else{
                        record.setSelectAnswer(record.getAnswerPath());
                    }
                }
            } else if ("consolidate".equals(record.getTestType())) {
                if("mp3".equals(record.getAnswerPath().substring(record.getAnswerPath().length()-3))){
                    record.setVoiceAnswer(record.getAnswerPath());
                    record.setVoiceDuration(getMp3Time(record.getAnswerPath())+" s");
                }else{
                    record.setSelectAnswer(record.getAnswerPath());
                }
            } else if(record.getTestType()!=null && record.getTestType().contains("学习")) {
                Integer tmp = Math.toIntExact((record.getQuestionId() - 1) % 4 + 1);
                record.setTimes(map.get(tmp));
                String[] strings = record.getTestType().split("-");
                record.setCounts(Integer.valueOf(strings[1]));
                record.setPromptsNumber(Integer.valueOf(strings[2]));
                record.setVoiceAnswer(record.getAnswerPath());
                record.setVoiceDuration(getMp3Time(record.getAnswerPath())+" s");
            }
        }
        return list;
    }

    private Long  getMp3Time(String path) {
        path = path.replace("/profile/","/home/ruoyi/uploadPath");
        try {
            MP3File f = (MP3File) AudioFileIO.read(new File(path));
            MP3AudioHeader audioHeader = (MP3AudioHeader) f.getAudioHeader();
            return Long.valueOf(audioHeader.getTrackLength());
        } catch (Exception e) {
            return 0L;
        }
    }

}
